const API_CODE = require("apiCode");
let apiCall = require("../../api/apiCall");
let PlayerData = require("PlayerData");

let enableVibrate = true;

let init = function () {
  cc.game.on(cc.game.EVENT_HIDE, (res) => {
    console.log('cc.game.EVENT_HIDE', res);
    if (cc.CEventManager.instance) {
      PlayerData.instance.saveExitTime();
      PlayerData.instance.saveData(true);
      cc.CEventManager.instance.dispatchEvent(EVENTS.GAME_ON_HIDE, res);
    }
  });
  
  cc.game.on(cc.game.EVENT_SHOW, (res) => {
    console.log('cc.game.EVENT_SHOW', res);
    if (cc.CEventManager.instance) {
      cc.CEventManager.instance.dispatchEvent(EVENTS.GAME_ON_SHOW, res);
    }
  });
  enableVibrate = LocalData.getItem('VibrateEnable', true);
};

//短震动
let vibrateShort = function () {
};
//长震动
let vibrateLong = function () {
};
let setVibrateEnable = function (enable) {
  enableVibrate = enable;
  LocalData.setItem('VibrateEnable', enableVibrate);
};

let isVibrateEnable = function () {
  return enableVibrate;
};

//显示游戏圈按钮
let showGameClubButton = function () {
};
//隐藏游戏圈按钮
let hideGameClubButton = function () {
};
let showOpenSettingButton = function () {
};

let hideOpenSettingButton = function () {
};

//分享给朋友
let shareToFriends = function (opts, shareParams) {
  cc.log(opts);
  let res = {"error": ''};
  if (opts.success) {
    opts.success(res);
  }
  
};

let checkGameUpdate = function () {

};
//用户登录
let login = function (callback) {
  Global.LoginState.isLogin = true;
  callback && callback(Global.LoginState);
};
//检查授权
let checkAuthorize = function (callback) {
  Global.LoginState.isAuthorize = true;
  callback && callback(Global.LoginState);
};

let loadUserInfo = function (callback) {
  Global.LoginState.isAuthorize = true;
  callback && callback(Global.LoginState);
};

let openSetting = function (callback) {
  callback && callback(Global.LoginState);
};

let showBannerAd = function (opts) {
  cc.log("showBannerAd", opts);
};
let hideBannerAd = function (clearStack) {
  cc.log("hideBannerAd", clearStack);
};
let setCurrentBannerStyle = function (opts) {
  cc.log("setCurrentBannerStyle", opts);
};
let showCurrentBanner = function () {
  cc.log("showCurrentBanner");
};

let hideCurrentBanner = function () {
  cc.log("hideCurrentBanner");
};
let isWaitingInterstitialAd = function () {
  return false;
};
let showVideoAd = function (logKey, callback) {
  let isEnd = true;
  callback && callback(isEnd);
};

let isVideoAdLoad = function () {
  return true;
};


/**
 * 插屏广告是否已经加载成功
 * @param logKey
 * @returns {boolean}
 */
let isInterstitialAdLoad = function (logKey) {
  cc.log("isInterstitialAdLoad", logKey);
  return false;
};

/**
 * 显示插屏广告
 * @param logKey
 * @param callback
 */
let showInterstitialAd = function (logKey, callback) {
  cc.log("showInterstitialAd", logKey);
  callback && callback();
};


let sendLogEvent = function (eventName, data) {
  cc.log("sendLogEvent", eventName, data);
};
let canSaveToAlbum = function (callback) {
  if (callback) {
    callback(true);
  }
};
let saveToAlbum = function (callback) {
  if (callback) {
    callback(null, true);
  }
};

let openSettingWithScope = function (callback) {
  callback && callback(true);
  
};

let openCustomerService = function (callback) {
  callback && callback();
};
let getOnShowRes = function () {
  return {
    query: {
      // award: "%7B%0A%20%20%22id%22%3A%20%22%E6%B5%8B%E8%AF%95id%22%2C%0A%20%20%22coin%22%3A%20100%2C%0A%20%20%22randomParts%22%3A%7B%0A%20%20%20%20%22count%22%3A%201%2C%0A%20%20%20%20%22notHold%22%3A%20false%0A%20%20%7D%2C%0A%20%20%22parts%22%3A%20%5B%0A%20%20%20%20%22face_001%22%2C%0A%20%20%20%20%22face_002%22%2C%0A%20%20%20%20%22face_003%22%2C%0A%20%20%20%20%22face_004%22%2C%0A%20%20%20%20%22face_005%22%2C%0A%20%20%20%20%22face_006%22%2C%0A%20%20%20%20%22face_007%22%0A%20%20%5D%0A%7D"
    }
  };
};

let postSubDomainMessage = function (msgType, data) {
};

let reportScores = function (opts) {
};

let loadGameData = function (callback) {
  apiCall.login((err, res) => {
    if (err) {
      Global.LoginState.gameServerLogin = false;
      callback && callback(Global.LoginState);
    } else {
      if (res.code === API_CODE.SUCCEED) {
        Global.LoginState.gameServerLogin = true;
        PlayerData.instance.setNetData(res);
        callback && callback(Global.LoginState);
      } else {
        Global.LoginState.gameServerLogin = false;
        callback && callback(Global.LoginState);
      }
    }
  });
};

let getPlatform = function () {
  return 'web';
};

let onStageStart = function (stageId, stageName) {
  cc.log("onStageStart", stageId, stageName);
};

let onStageRunning = function (stageId, stageName, eventString, itemName, itemDesc) {
  cc.log("onStageRunning", stageId, stageName, eventString, itemName, itemDesc);
};

let onStageEnd = function (stageId, stageName, complete, descString) {
  cc.log("onStageEnd", stageId, stageName, complete, descString);
};

let showFollowMPQRCode = function (callback) {
  callback && callback();
};


let showGameBanner = function (opts) {
  cc.log("showGameBanner", opts);
};
let hideGameBanner = function (clearStack) {
  cc.log("hideGameBanner", clearStack);
};
let setCurrentGameBannerStyle = function (opts) {
  cc.log("setCurrentGameBannerStyle", opts);
};
let showCurrentGameBanner = function () {
  cc.log("showCurrentGameBanner");
};

let hideCurrentGameBanner = function () {
  cc.log("hideCurrentGameBanner");
};

let showGameIcon = function (opts) {
  cc.log("showGameIcon", opts);
};
let hideGameIcon = function (clearStack) {
  cc.log("hideGameIcon", clearStack);
};


/**
 * 是否支持原生广告
 * @returns {boolean}
 */
let isSupportNativeAd = function () {
  return true;
};
/**
 * 上报原生广告曝光
 * @param adData
 */
let reportNativeAdShow = function (adData) {
  console.log("reportAdShow", adData);
};

/**
 * 上报广告点击，
 * @param adData
 */
let reportNativeAdClick = function (adData) {
  console.log("reportAdClick", adData);
};

let isNativeAdLoaded = function () {
  return false;
};

let getNativeAdData = function () {
  return null;
};

let destroyNativeAdDataAndPreloadNext = function (adData) {

};

let installShortcut = function (callback) {
  callback && callback(true);
};

let hasShortcutInstalled = function (callback) {
  callback && callback(true);
};

/**
 * 设置游戏加载进度页面
 * @param progress 0-100 的整数
 */
let setLoadingProgress = function (progress) {
};

/**
 * 隐藏游戏加载进度页面
 * @param callback
 */
let loadingComplete = function (callback) {
  callback && callback();
};

let initBoxAd = function () {
};


let isSupportAppBoxAd = function () {
  return false;
};

let isAppBoxAdLoaded = function () {
  return false;
};

//显示盒子广告
let showAppBoxAd = function (force, callback) {
  callback && callback();
};

//隐藏盒子广告
let hideAppBoxAd = function () {
};


module.exports = {
  init,
  vibrateShort,
  vibrateLong,
  setVibrateEnable,
  isVibrateEnable,
  showGameClubButton,
  hideGameClubButton,
  shareToFriends,
  login,
  checkAuthorize,
  loadUserInfo,
  showOpenSettingButton,
  hideOpenSettingButton,
  openSetting,
  checkGameUpdate,
  showVideoAd,
  isVideoAdLoad,
  showBannerAd,
  hideBannerAd,
  setCurrentBannerStyle,
  showCurrentBanner,
  hideCurrentBanner,
  sendLogEvent,
  canSaveToAlbum,
  saveToAlbum,
  openSettingWithScope,
  openCustomerService,
  getOnShowRes,
  postSubDomainMessage,
  reportScores,
  loadGameData,
  getPlatform,
  onStageStart,
  onStageRunning,
  onStageEnd,
  isInterstitialAdLoad,
  showInterstitialAd,
  isWaitingInterstitialAd,
  showGameBanner,
  hideGameBanner,
  setCurrentGameBannerStyle,
  showCurrentGameBanner,
  hideCurrentGameBanner,
  showGameIcon,
  hideGameIcon,
  isSupportNativeAd,
  reportNativeAdShow,
  reportNativeAdClick,
  isNativeAdLoaded,
  getNativeAdData,
  destroyNativeAdDataAndPreloadNext,
  installShortcut,
  hasShortcutInstalled,
  setLoadingProgress,
  loadingComplete,
  initBoxAd,
  isSupportAppBoxAd,
  isAppBoxAdLoaded,
  showAppBoxAd,
  hideAppBoxAd,
};